//global sumstats_start \begin{threeparttable}[b] \begin{tabular}{l | l l l | l l l}\hline\hline & \multicolumn{3}{c}{\bf OLS} & \multicolumn{3}{c}{\bf 2SLS}\\\hline \text{Exports to...}  & $ log(exp) $ & $ log(exp+1) $ & $ asinh(exp) $  &  $ log(exp) $ & $ log(exp+1) $ & $ asinh(exp) $\\\hline
global sumstats_start \begin{threeparttable}[b] \begin{tabular}{l | l l l}\hline\hline \text{Exports to...}  & $ log(exp) $ & $ log(exp+1) $ & $ asinh(exp) $  \\\hline
global sumstats_end  \hline \end{tabular} \begin{tablenotes} \item {\footnotesize \tablenote}  \item {\footnotesize \$^{*}p<0.10, ^{**}p<0.05, ^{***}p<0.01\$} \end{tablenotes} \end{threeparttable}
cap file close fh
file open fh using "${path}tables/DIDToRegions.tex", write replace
/* write table header to file*/
file write fh "$sumstats_start" _n
foreach v in weurope eeurope namerica easiapac meastnafr lac ssa {
    preserve
    //For western Europe we make sure to use all exports not to eliminate Germany twice. For the rest of the world we use expots excl. to Germany as baseline
    if "`v'" == "weurope" {
        g expexcl`v' = exp - expto`v'
    }
    else {
        g expexcl`v' = exp_xdeu-expto`v'
    }
    
    local lbl : variable label expto`v'
    label var expexcl`v' "`lbl'"

    local y1min2 = $y1-2
    local y2plus2 = $y2+2
    egen temp1 = total(expto`v') if year>=`y1min2' & year<=$y1, by(product)
    egen temp2 = total(expto`v') if year>=$y2 & year<=`y2plus2', by(product)
    //egen temp1 = total(expexcl`v') if year>=`y1min2' & year<=$y1, by(product)
    //egen temp2 = total(expexcl`v') if year>=$y2 & year<=`y2plus2', by(product)
    g cumexp = temp1/3 if year==$y1
    replace cumexp = temp2/3 if year==$y2
    drop temp1 temp2
    qui replace lnexp = log(cumexp)
    qui replace lnexpplus1 = log(cumexp+1)
    qui replace asinhexp = asinh(cumexp)

    foreach dp in lnexp lnexpplus1 asinhexp {
        qui reghdfe `dp' c.treat2000#c.$after if $didsample, $rhsoptions
        matrix A = e(b)
        matrix V = e(V)
        local b`dp' = A[1,1]
        local sd`dp' = sqrt(V[1,1])
        local stars`dp' ""
        if A[1,1]/`sd`dp''>=1.645 {
            local stars`dp' "*"
        }
        if A[1,1]/`sd`dp''>=1.96 {
            local stars`dp' "**"
        }
        if A[1,1]/`sd`dp''>=2.576 {
            local stars`dp' "***"
        }

        qui reghdfe `dp' (c.treat2000#c.$after=c.treatIV#c.$after)  if $didsample, $rhsoptions
        matrix A = e(b)
        matrix V = e(V)
        local bIV`dp' = A[1,1]
        local sdIV`dp' = sqrt(V[1,1])
        local starsIV`dp' ""
        if A[1,1]/`sdIV`dp''>=1.645 {
            local starsIV`dp' "*"
        }
        if A[1,1]/`sdIV`dp''>=1.96 {
            local starsIV`dp' "**"
        }
        if A[1,1]/`sdIV`dp''>=2.576 {
            local starsIV`dp' "***"
        }
    }
    local row = subinstr("`lbl'", "&","\&",.)
    //file write fh "`row'" "&" %9.3f (`blnexp') "`starslnexp'"  "&" %9.3f (`blnexpplus1') "`starslnexpplus1'"  "&" %9.3f (`basinhexp') "`starsasinhexp'"  "&" %9.3f (`bIVlnexp') "`starsIVlnexp'"  "&" %9.3f (`bIVlnexpplus1') "`starsIVlnexpplus1'"  "&" %9.3f (`bIVasinhexp') "`starsIVasinhexp'" "\\" _n
    file write fh "`row'" "&" %9.3f (`blnexp') "`starslnexp'"  "&" %9.3f (`blnexpplus1') "`starslnexpplus1'"  "&" %9.3f (`basinhexp') "`starsasinhexp'" "\\" _n
    ///file write fh "" "&" "(" %9.3f (`sdlnexp') ")" "&" "("  %9.3f (`sdlnexpplus1') ")" "&" "(" %9.3f (`sdasinhexp') ")" "&" "(" %9.3f (`sdIVlnexp') ")" "&" "("  %9.3f (`sdIVlnexpplus1') ")" "&" "(" %9.3f (`sdIVasinhexp') ")"  "\\" _n
    file write fh "" "&" "(" %9.3f (`sdlnexp') ")" "&" "("  %9.3f (`sdlnexpplus1') ")" "&" "(" %9.3f (`sdasinhexp') ")"  "\\" _n
    di "Writing `row'..."
    restore
}
file write fh "$sumstats_end" _n
file close fh
